<?php
/**
 * Date: 10/22/12
 * Time: 9:25 PM
 * User: Amitav Roy
 */
class Transactions extends CI_Controller {
	function __construct() {
		parent::__construct();

		// This is restricted area.
		auth_user();
	}

  /**
   * Index page will always redirect to home.
   */
	function index() {
		// Redirecting to view page.
		redirect('transactions/view');
	}

  /**
   * This is the form for adding the transaction.
   */
	function add() {
    // Adding the general data for the view
		$data['pagetitle'] = 'Add Transaction';
		$data['subheading'] = 'Add New Transaction to the system';
		$data['scripts']['transaction'] = 'transaction'; // Including the javascript.
		$data['viewname'] = 'addtransaction_v.php';
		$data['viewdata'] = 'A';

		$this->load->view('master', $data);
	}

	/**
	 * This is where the actual data is getting saved.
	 */
	function do_add() {
		if (isset($_POST) && $_POST['save'] == 'Save') {
			$this->load->model('transaction_m','trans');
			$this->trans->add_transaction();

			// Set the message for the user
			set_message('Your expense is successfully added.');

			// Redirect the user to the page
      redirect('transactions/add');
		}
		else {
			redirect('transactions/add');
		}
	}

  /**
   * This is an ajax page. This is being used to get the terms 
   * - the sub category when a main category is selected in the 
   * drop down when entering the transaction.
   */
	function get_terms() {
		if (isset($_POST)) {
			$result = $this->global_model->global_get_term($this->input->post('cid'));

			$options = array();
			foreach ($result as $term) {
				$options[$term['tid']] = $term['name'];
			}

			$this->load->helper('form');
			print form_dropdown('terms',$options);
		}
		else {
			redirect('transactions/add');
		}
	}

  /**
   * This is the page to show the list of transactions.
   */
  function view() {
	  $this->load->model('transaction_m');

    $data['pagetitle'] = 'View Transactions';
    $data['subheading'] = 'View the transactions';
    $data['scripts']['transaction'] = 'transaction';
    $data['viewname'] = 'viewtransaction_v.php';
    $data['viewdata']['trans'] = $this->transaction_m->view_expenses();
	  $data['viewdata']['rec_count'] = $this->transaction_m->expense_count();

    $this->load->view('master', $data);
  }

	/**
	 * This is the function to generate the edit ajax call
	 * for generating the edit modal form for transactions.
	 */
	function edit_expense_view() {
		if (isset($_POST) && isset($_POST['valid'])) {
			// Loading the model.
			$this->load->model('transaction_m');

			// Expense id from post.
			$expense_id = $_POST['eid'];

			// Data to pass to the view.
			$data['data'] = $this->transaction_m->expense_entry_details($expense_id);

			// Loading the view with the data.
			$this->load->view('edittransaction_v',$data);
		}
		else {
			redirect('transactions/index');
		}
	}

	/**
	 * This is the page where the edit data is coming
	 * and the model is called to update the database.
	 */
	function edit_expense() {
		if (isset($_POST) && $_POST['save'] == 'Save') {
			// Loading the model.
			$this->load->model('transaction_m');

			$this->transaction_m->update_expense($_POST);

			redirect('transactions/view');
		}
		else {
			redirect('transactions/index');
		}
	}
}

/* End of file transactions.php */
/* Location: ./application/controllers/transactions.php */